package com.wenx.v3auth.config;

import com.wenx.v3auth.service.CompositeUserDetailsService;
import com.wenx.v3auth.service.PlatformUserDetailsService;
import com.wenx.v3auth.service.V3CustomUserDetailsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * 用户详情服务配置
 * 
 * 配置多用户类型认证支持：
 * - 系统用户认证服务（V3CustomUserDetailsService）
 * - 平台用户认证服务（PlatformUserDetailsService）
 * - 复合用户认证服务（CompositeUserDetailsService）作为主要服务
 * 
 * @author wenx
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class DetailsServiceConfig {

    @Bean()
    @Primary
    public UserDetailsService compositeUserDetailsService(
            @Qualifier("v3CustomUserDetailsService") UserDetailsService systemUserDetailsService,
            @Qualifier("platformUserDetailsService") UserDetailsService platformUserDetailsService) {
        
        log.info("配置复合用户详情服务，支持系统用户和平台用户认证");
        log.info("系统用户认证服务: {}", systemUserDetailsService.getClass().getSimpleName());
        log.info("平台用户认证服务: {}", platformUserDetailsService.getClass().getSimpleName());
        
        return new CompositeUserDetailsService(systemUserDetailsService, platformUserDetailsService);
    }

    @Bean("systemUserDetailsService")
    public UserDetailsService systemUserDetailsService(V3CustomUserDetailsService v3CustomUserDetailsService) {
        log.debug("配置系统用户详情服务: {}", v3CustomUserDetailsService.getClass().getSimpleName());
        return v3CustomUserDetailsService;
    }

    @Bean("platformUserDetailsServiceBean")
    public UserDetailsService platformUserDetailsServiceBean(PlatformUserDetailsService platformUserDetailsService) {
        log.debug("配置平台用户详情服务: {}", platformUserDetailsService.getClass().getSimpleName());
        return platformUserDetailsService;
    }
}